{extend name="common/base"/}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-page">
	{if condition="$id eq 0"}
	<h3 class="pb-3">新增管理员</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">登录账号<font>*</font></td>
			<td><input type="text" name="username" lay-verify="required" placeholder="请输入登录账号" lay-reqText="请输入登录账号" class="layui-input"></td>
			<td class="layui-td-gray">用户名<font>*</font></td>
			<td><input type="text" name="nickname" lay-verify="required" placeholder="请输入用户名" lay-reqText="请输入用户名" class="layui-input"></td>
			<td rowspan="3" class="layui-td-gray-2">头像<br /><span style="font-size: 12px;">(如若不上传<br />系统将自动生成)</span></td>
			<td rowspan="3" valign="top" style="width: 160px;">
				<div class="layui-upload" style="width: 100px; margin:0 auto">
					<div class="layui-upload-list" id="demo1" style="width: 100%; height:100px;">
						<img src="/static/admin/images/nonepic600x600.jpg" height="100" style="max-width: 100%; width: 100%;" />
						<input type="hidden" name="thumb" value="">
					</div>
					<button type="button" class="layui-btn layui-btn-normal" style="width: 100%;" id="uploadBtn">上传头像</button>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">登录密码<font>*</font></td>
			<td><input type="password" name="pwd" lay-verify="required" placeholder="请输入密码" lay-reqText="请输入密码" class="layui-input"></td>
			<td class="layui-td-gray">确认密码<font>*</font></td>
			<td><input type="password" name="pwd_confirm" lay-verify="required" placeholder="请再次输入密码" lay-reqText="请再次输入密码" class="layui-input"></td>
		</tr>
		<tr>
			<td class="layui-td-gray">手机号码<font>*</font></td>
			<td><input type="text" name="mobile" lay-verify="required" placeholder="请输入手机" lay-reqText="请输入手机" class="layui-input"></td>
			<td class="layui-td-gray">电子邮箱</td>
			<td><input type="text" name="email" placeholder="电子邮箱" class="layui-input"></td>
		</tr>
		<tr>
			<td class="layui-td-gray">所在部门<font>*</font></td>
			<td>
				<select name="did" lay-verify="required" lay-reqText="请选择所属部门">
					<option value="">请选择所属部门</option>
					{volist name="department" id="v"}
						<option value="{$v.id}">{$v.title}</option>
					{/volist}
				</select>
			</td>
			<td class="layui-td-gray">岗位职称<font>*</font></td>
			<td>
				<select name="position_id" lay-verify="required" lay-reqText="请选择岗位职称">
					<option value="">请选择岗位职称</option>
					{volist name="position" id="v"}
						<option value="{$v.id}">{$v.title}</option>
					{/volist}
				</select>				
			</td>
			<td class="layui-td-gray">用户状态<font>*</font></td>
			<td>
				<input type="radio" name="status" value="1" title="正常" checked>
				<input type="radio" name="status" value="0" title="禁用">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">用户角色<font>*</font></td>
			<td colspan="5">
				{volist name=":get_admin_group()" id="v"}
				<input type="checkbox" name="group_id[]" title="{$v.title}" value="{$v.id}" lay-skin="primary">
				{/volist}
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">备注信息</td>
			<td colspan="5"><textarea name="desc" placeholder="请输入备注" class="layui-textarea"></textarea></td>
		</tr>
	</table>
	{else/}
	<h3 class="pb-3">编辑管理员</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">登录账号<font>*</font></td>
			<td>
				<input type="hidden" name="id" value="{$admin.id}">
				<input type="text" name="username" lay-verify="required" placeholder="请输入登录账号" lay-reqText="请输入登录账号" class="layui-input" value="{$admin.username}">
			</td>
			<td class="layui-td-gray">用户名<font>*</font></td>
			<td><input type="text" name="nickname" lay-verify="required" placeholder="请输入用户名" lay-reqText="请输入用户名" class="layui-input" value="{$admin.nickname}"></td>
			<td rowspan="3" class="layui-td-gray-2">头像<br /><span style="font-size: 12px;">(如若不上传<br />系统将自动生成)</span></td>
			<td rowspan="3" valign="top" style="width: 160px;">
				<div class="layui-upload" style="width: 100px; margin:0 auto">
					<div class="layui-upload-list" id="demo1" style="width: 100%; height:100px; overflow: hidden;">
						<img src="{$admin.thumb}" height="100" style="max-width: 100%; width: 100%;" />
						<input type="hidden" name="thumb" value="{$admin.thumb}">
					</div>
					<button type="button" class="layui-btn layui-btn-normal" style="width: 100%;" id="uploadBtn">上传头像</button>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">登录密码</td>
			<td><input type="password" name="edit_pwd" placeholder="请输入密码" class="layui-input"></td>
			<td class="layui-td-gray">确认密码</td>
			<td><input type="password" name="edit_pwd_confirm" placeholder="请再次输入密码" class="layui-input"></td>
		</tr>
		<tr>
			<td class="layui-td-gray">手机号码<font>*</font></td>
			<td><input type="text" name="mobile" lay-verify="required" placeholder="请输入手机" lay-reqText="请输入手机" class="layui-input" value="{$admin.mobile==0?'':$admin.mobile}"></td>
			<td class="layui-td-gray">电子邮箱</td>
			<td><input type="text" name="email" placeholder="电子邮箱" class="layui-input" value="{$admin.email}"></td>
		</tr>
		<tr>
			<td class="layui-td-gray">所在部门<font>*</font></td>
			<td>
				<select name="did" lay-verify="required" lay-reqText="请选择所属部门">
					<option value="">请选择所属部门</option>
					{volist name="department" id="v"}
						<option value="{$v.id}" {eq name="$v.id" value="$admin.did"}selected{/eq}>{$v.title}</option>
					{/volist}
				</select>
			</td>
			<td class="layui-td-gray">岗位职称<font>*</font></td>
			<td>
				<select name="position_id" lay-verify="required" lay-reqText="请选择岗位职称">
					<option value="">请选择岗位职称</option>
					{volist name="position" id="v"}
						<option value="{$v.id}" {eq name="$v.id" value="$admin.position_id"}selected{/eq}>{$v.title}</option>
					{/volist}
				</select>				
			</td>
			<td class="layui-td-gray">用户状态<font>*</font></td>
			<td>
				<input type="radio" name="status" value="1" title="正常" {eq name="$admin.status" value="1" }checked{/eq}>
				<input type="radio" name="status" value="0" title="禁用" {eq name="$admin.status" value="0"}checked{/eq}>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">用户角色<font>*</font></td>
			<td colspan="5">
				{volist name=":get_admin_group()" id="v"}
				<input type="checkbox" name="group_id[]" title="{$v.title}" value="{$v.id}" {volist name="$admin.group_id" id="a" }{eq name="$a" value="$v.id" }checked{/eq}{/volist} lay-skin="primary">
				{/volist}
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">备注信息</td>
			<td colspan="5"><textarea name="desc" placeholder="请输入备注" class="layui-textarea">{$admin.desc|default=""}</textarea></td>
		</tr>
	</table>
	{/if}
	<div class="py-3">
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
	</div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool'];
	function gouguInit() {
		var form = layui.form,tool=layui.tool,upload = layui.upload;
		//头像上传
		var uploadInst = upload.render({
			elem: '#uploadBtn'
			, url: '/admin/api/upload'
			, done: function (e) {
				//如果上传失败
				if (e.code == 1) {
					return layer.msg('上传失败');
				}
				//上传成功
				$('#demo1 input').attr('value', e.data.filepath);
				$('#demo1 img').attr('src', e.data.filepath);
			}
		});

		//监听提交
		form.on('submit(webform)', function (data) {
			let callback = function (e) {
				layer.msg(e.msg);
				if (e.code == 0) {
					parent.layui.tool.close(1000);
				}
			}
			tool.post("/admin/admin/add", data.field, callback);
			return false;
		});
	}

</script>
{/block}
<!-- /脚本 -->